<div class="tutorial-bundle" data-metadata-json='{"difficulty": "medium", "groupedProjectAssets": [{"name": "green LED", "assets": ["light-off.png", "light-on.png"]}, {"name": "neon texts", "assets": ["YES-neon-text.png", "FAIL-neon-text.png"]}]}' data-tip-sha1="c9b58e9254874e66bbbd5dcc2c4eb9382b8e1efe"><div class="front-matter" data-complete-code-text='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        if user_attempt == pattern_start:
            if user_attempt_length == len(pattern):
                pytch.broadcast_and_wait("correct")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            pressing_allowed = False
            pytch.broadcast("fail")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()

    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()

    @pytch.when_I_receive("fail")
    def flash_fail(self):
        self.switch_costume("FAIL-neon-text")
        self.show()
' data-initial-code-text="import pytch
"><h1>Trimon</h1><p>We’ll make a memory game where the player has to remember longer and
longer patterns of flashing lights.  Don’t get one wrong!</p></div><div class="chapter-content"><h2>Creating the backdrop</h2><p>We’ll start by adding a Stage with a simple blue gradient backdrop:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]
' data-slug="add-background-and-backdrop"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>4</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>5</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr></tbody></table></div></div><p>If you run your program after adding these lines, you should see the
background.</p></div><div class="chapter-content"><h2>Making the first light</h2><p>Our game will have three lights.  We’ll start by adding just one of
them.  Once we have that working, we’ll copy and paste the code, with
small changes, to make the other lights.  (This isn’t usually the best
way to make a set of similar things, but the other ways are more
advanced than we can cover in this tutorial.)</p><p>The light is called an ‘LED’ in the code, because that’s the sort of
light the image looks most like.  It needs two costumes, one for when
it’s just sitting there, and another for when it’s lit up:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
' data-slug="add-LED1-and-costumes"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>5</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>8</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>We want to arrange the LEDs in a line across the screen, so at the
start of the game, this LED should move to a position a bit to the
left of centre, and a bit above centre:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)
' data-slug="init-LED1-position"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)
"><tr><td class="linenum"></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>        self.go_to_xy(-140, 80)</pre></td></tr></tbody></table></div></div><p>The light should be able to make a noise, so we’ll add a sound:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)
' data-slug="add-LED1-sound"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    Sounds = ["note-1.mp3"]
'><tr><td class="linenum"></td><td class="linenum"><pre>10</pre></td><td><pre>    Sounds = ["note-1.mp3"]</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>11</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>13</pre></td><td><pre>    def move_to_position(self):</pre></td></tr></tbody></table></div></div><p>And finally (for now), we’ll make the light flash and make its sound
when we click on it:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)
' data-slug="flash-LED1-on-click"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre>        self.go_to_xy(-140, 80)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='
    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)
'><tr><td class="linenum"></td><td class="linenum"><pre>15</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>16</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>17</pre></td><td><pre>    def flash(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre>        self.switch_costume(1)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>19</pre></td><td><pre>        self.play_sound_until_done("note-1")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>20</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr></tbody></table></div></div><p>I’m saying which costume to switch to based on position in the list,
where the first thing in the list is position zero.  You could use the
costume names (<code>"light-off"</code> and <code>"light-on"</code>) if you prefer.</p><h3>Try this now!</h3><p>In the final game, it won’t be clicking that sets off the flash and
sound, but by writing this code, we can test what we’ve done so far.
We’ll change the code later.</p></div><div class="chapter-content"><h2>Making the other lights</h2><p>The other lights are fairly similar, so we’ll go through this more
quickly.  For now we’ll just add the basics of these two.</p><p>First the middle light.  Its costumes and sounds are very similar to
the left-hand light, except this light will make a different sound:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]
' data-slug="add-LED2-and-costumes-sounds"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>18</pre></td><td><pre>        self.switch_costume(1)</pre></td></tr><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>19</pre></td><td><pre>        self.play_sound_until_done("note-1")</pre></td></tr><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>20</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]
'><tr><td class="linenum"></td><td class="linenum"><pre>21</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>22</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>23</pre></td><td><pre>class LED2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>24</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>25</pre></td><td><pre>    Sounds = ["note-2.mp3"]</pre></td></tr></tbody></table></div></div><p>The right-hand light is also similar:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]
' data-slug="add-LED3-and-costumes-sounds"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>23</pre></td><td><pre>class LED2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>24</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>25</pre></td><td><pre>    Sounds = ["note-2.mp3"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]
'><tr><td class="linenum"></td><td class="linenum"><pre>26</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>27</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>28</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>29</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>30</pre></td><td><pre>    Sounds = ["note-3.mp3"]</pre></td></tr></tbody></table></div></div><p>At the start of the game, the middle light should move to its
position:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]
' data-slug="init-LED2-position"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>24</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>25</pre></td><td><pre>    Sounds = ["note-2.mp3"]</pre></td></tr><tr><td class="linenum"><pre>26</pre></td><td class="linenum"><pre>26</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

"><tr><td class="linenum"></td><td class="linenum"><pre>27</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>28</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>29</pre></td><td><pre>        self.go_to_xy(0, 80)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>30</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>27</pre></td><td class="linenum"><pre>31</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>28</pre></td><td class="linenum"><pre>32</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>29</pre></td><td class="linenum"><pre>33</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>And the right-hand light to <em>its</em> position:</p><div class="patch-container" data-code-as-of-commit='import pytch


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)
' data-slug="init-LED3-position"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>32</pre></td><td class="linenum"><pre>32</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>33</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>34</pre></td><td><pre>    Sounds = ["note-3.mp3"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)
"><tr><td class="linenum"></td><td class="linenum"><pre>35</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>36</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>37</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>38</pre></td><td><pre>        self.go_to_xy(140, 80)</pre></td></tr></tbody></table></div></div><p>We won’t bother with the testing ‘click to flash and make a noise’
code for these two.  We’ll put the proper code in later.</p></div><div class="chapter-content"><h2>Storing the pattern in a variable</h2><p>Now we’ll think about how to keep track of the pattern which the
player has to repeat.  We’ll give each light a number:</p><ul>
<li><code>1</code> — left-hand light</li>
<li><code>2</code> — middle light</li>
<li><code>3</code> — right-hand light</li>
</ul><p>and store the pattern in a variable holding a Python <em>list</em>, which is
perfect for this job.</p><p>Soon we’ll write the code which shows the pattern, and we want to be
able to test that code without having to write all the rest of the
game.  So we’ll cheat, and start off with a short pattern.  At the top
of the program, we’ll define a variable holding a pattern we can test
with:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)
' data-slug="define-pattern-global-test-value"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="pattern = [1, 2, 3, 1]

"><tr><td class="linenum"></td><td class="linenum"><pre>3</pre></td><td><pre>pattern = [1, 2, 3, 1]</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>4</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>5</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>6</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>7</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr></tbody></table></div></div><p>When we’ve written the rest of the code, this list should mean the
pattern</p><ul>
<li>left, middle, right, left.</li>
</ul><p>plays.</p></div><div class="chapter-content"><h2>Playing the pattern on the lights</h2><p>We need to think about whose job it is to control the showing of the
pattern.  It involves more than one light, so we’ll put the code in
our <code>Background</code>.</p><p>The <code>Background</code> will tell a particular light to flash by broadcasting
a message:</p><ul>
<li>To tell the <strong>left-hand</strong> light to flash, it’ll broadcast <strong><code>"flash-1"</code></strong>.</li>
<li>To tell the <strong>middle</strong> light to flash, it’ll broadcast <strong><code>"flash-2"</code></strong>.</li>
<li>To tell the <strong>right-hand</strong> light to flash, it’ll broadcast <strong><code>"flash-3"</code></strong>.</li>
</ul><p>We’ll use <code>broadcast_and_wait()</code> to make sure each light finishes its
flash before we tell the next light to do its flash.</p><p>We want to be able to test this code, so for now we’ll make it happen
when we press a key.  In the full game, this will be different.</p><p>Our code should go through each number in the <code>pattern</code> list in turn,
and broadcast the correct message.  The message should be the string
<code>"flash-"</code> joined on to the light number.  Suppose we have a variable
<code>led</code> which is the number <code>1</code>, <code>2</code>, or <code>3</code>.  Then we can turn that
number into a string by saying <code>str(led)</code>.  We can glue the two
strings together with <code>+</code>, and give a name to the string we get, by
saying</p><pre><code class="language-python">message = "flash-" + str(led)
</code></pre><p>To go through the numbers in the <code>pattern</code> list one by one, we can use
a useful feature of Python made for exactly this job, the</p><pre><code class="language-python">for thing in list_of_things:
    # Do something with thing
</code></pre><p>loop.</p><p>Putting this all together, we get:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_this_sprite_clicked
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)
' data-slug="play-pattern-on-keypress"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>6</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)

'><tr><td class="linenum"></td><td class="linenum"><pre>9</pre></td><td><pre>    @pytch.when_key_pressed("p")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>10</pre></td><td><pre>    def play_pattern(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>        for led in pattern:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>            message = "flash-" + str(led)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>            pytch.broadcast_and_wait(message)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>14</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>15</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>16</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>17</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>But if you run this and try pressing <code>p</code>, nothing will happen, because
we haven’t told the lights to react to those messages yet.</p></div><div class="chapter-content"><h2>Making the lights flash via messages</h2><p>We already have working code to make the left-hand light flash and
make its sound.  We just need to use a different ‘hat block’, to tell
Pytch to run this code when the message <code>"flash-1"</code> is received.  So
delete the <code>when_this_sprite_clicked</code> line and replace it with a
<code>when_I_receive</code> line:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)
' data-slug="flash-LED1-on-message"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>21</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>22</pre></td><td><pre>        self.go_to_xy(-140, 80)</pre></td></tr><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>23</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>24</pre></td><td class="linenum"></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_I_receive("flash-1")
'><tr><td class="linenum"></td><td class="linenum"><pre>24</pre></td><td><pre>    @pytch.when_I_receive("flash-1")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>25</pre></td><td><pre>    def flash(self):</pre></td></tr><tr><td class="linenum"><pre>26</pre></td><td class="linenum"><pre>26</pre></td><td><pre>        self.switch_costume(1)</pre></td></tr><tr><td class="linenum"><pre>27</pre></td><td class="linenum"><pre>27</pre></td><td><pre>        self.play_sound_until_done("note-1")</pre></td></tr></tbody></table></div></div><p>Now this block of code is ready to copy and paste into the code for
the other two lights.  First we’ll copy it into <code>LED2</code>.  There are two
changes:</p><ul>
<li>The message it reacts to is <strong><code>"flash-2"</code></strong> (not <code>"flash-1"</code>).</li>
<li>The sound it plays is <strong><code>"note-2"</code></strong> (not <code>"note-1"</code>).</li>
</ul><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)
' data-slug="flash-LED2-on-message"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>36</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        self.go_to_xy(0, 80)</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>38</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)

'><tr><td class="linenum"></td><td class="linenum"><pre>39</pre></td><td><pre>    @pytch.when_I_receive("flash-2")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>40</pre></td><td><pre>    def flash(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>41</pre></td><td><pre>        self.switch_costume(1)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>42</pre></td><td><pre>        self.play_sound_until_done("note-2")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>43</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>44</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>45</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>46</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>47</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>And for <code>LED3</code>, we copy, paste, and change:</p><ul>
<li>React to message <strong><code>"flash-3"</code></strong>.</li>
<li>Play sound <strong><code>"note-3"</code></strong>.</li>
</ul><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="flash-LED3-on-message"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>50</pre></td><td class="linenum"><pre>50</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>51</pre></td><td class="linenum"><pre>51</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"><pre>52</pre></td><td class="linenum"><pre>52</pre></td><td><pre>        self.go_to_xy(140, 80)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='
    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
'><tr><td class="linenum"></td><td class="linenum"><pre>53</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>54</pre></td><td><pre>    @pytch.when_I_receive("flash-3")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>55</pre></td><td><pre>    def flash(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>56</pre></td><td><pre>        self.switch_costume(1)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>57</pre></td><td><pre>        self.play_sound_until_done("note-3")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>58</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr></tbody></table></div></div><p>If you try the program now, our test pattern (left, middle, right,
left) should play when you press the <code>p</code> key.</p><p>It would look better with a small pause between each flash, so we’ll
add a line to the loop in <code>Background</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="small-delay-between-pattern-flashes"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre>        for led in pattern:</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>            message = "flash-" + str(led)</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>            pytch.broadcast_and_wait(message)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="            pytch.wait_seconds(0.1)
"><tr><td class="linenum"></td><td class="linenum"><pre>14</pre></td><td><pre>            pytch.wait_seconds(0.1)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>15</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>17</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Adding the buttons</h2><p>Next, the game needs three buttons, so the player can try to repeat
the patterns.</p><h3>Left-hand button</h3><p>We’ll add a sprite for each button, starting with the left-hand one:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="add-Button1-and-costume"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>29</pre></td><td class="linenum"><pre>29</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr><tr><td class="linenum"><pre>30</pre></td><td class="linenum"><pre>30</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>31</pre></td><td class="linenum"><pre>31</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]


'><tr><td class="linenum"></td><td class="linenum"><pre>32</pre></td><td><pre>class Button1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>33</pre></td><td><pre>    Costumes = ["green-button.png"]</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>34</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>35</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>32</pre></td><td class="linenum"><pre>36</pre></td><td><pre>class LED2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>37</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>38</pre></td><td><pre>    Sounds = ["note-2.mp3"]</pre></td></tr></tbody></table></div></div><p>It should start the game by moving to a position underneath the
left-hand light:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="init-Button1-position"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>32</pre></td><td class="linenum"><pre>32</pre></td><td><pre>class Button1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>33</pre></td><td><pre>    Costumes = ["green-button.png"]</pre></td></tr><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>34</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

"><tr><td class="linenum"></td><td class="linenum"><pre>35</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>36</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>37</pre></td><td><pre>        self.go_to_xy(-140, -80)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>38</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>39</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>40</pre></td><td><pre>class LED2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>41</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>And when it’s clicked, it should make the left-hand light flash and
make a sound.  We can do this by broadcasting the correct message:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="handle-Button1-click-v1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>36</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        self.go_to_xy(-140, -80)</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>38</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")

'><tr><td class="linenum"></td><td class="linenum"><pre>39</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>40</pre></td><td><pre>    def press_button(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>41</pre></td><td><pre>        pytch.broadcast_and_wait("flash-1")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>42</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>43</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>44</pre></td><td><pre>class LED2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>45</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><h3>Middle button</h3><p>This is very similar.  The <code>Button2</code> sprite has the same costume:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="add-Button2-and-costume"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>56</pre></td><td class="linenum"><pre>56</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr><tr><td class="linenum"><pre>57</pre></td><td class="linenum"><pre>57</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>58</pre></td><td class="linenum"><pre>58</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]


'><tr><td class="linenum"></td><td class="linenum"><pre>59</pre></td><td><pre>class Button2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>60</pre></td><td><pre>    Costumes = ["green-button.png"]</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>61</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>62</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>59</pre></td><td class="linenum"><pre>63</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>60</pre></td><td class="linenum"><pre>64</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr><tr><td class="linenum"><pre>61</pre></td><td class="linenum"><pre>65</pre></td><td><pre>    Sounds = ["note-3.mp3"]</pre></td></tr></tbody></table></div></div><p>but moves to a different position at the start of the game:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="init-Button2-position"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>59</pre></td><td class="linenum"><pre>59</pre></td><td><pre>class Button2(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>60</pre></td><td class="linenum"><pre>60</pre></td><td><pre>    Costumes = ["green-button.png"]</pre></td></tr><tr><td class="linenum"><pre>61</pre></td><td class="linenum"><pre>61</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

"><tr><td class="linenum"></td><td class="linenum"><pre>62</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>63</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>64</pre></td><td><pre>        self.go_to_xy(0, -80)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>65</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>62</pre></td><td class="linenum"><pre>66</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>63</pre></td><td class="linenum"><pre>67</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>64</pre></td><td class="linenum"><pre>68</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>and broadcasts the right message to make the middle light flash and
make its sound:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-2")


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)
' data-slug="handle-Button2-click-v1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>63</pre></td><td class="linenum"><pre>63</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"><pre>64</pre></td><td class="linenum"><pre>64</pre></td><td><pre>        self.go_to_xy(0, -80)</pre></td></tr><tr><td class="linenum"><pre>65</pre></td><td class="linenum"><pre>65</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-2")

'><tr><td class="linenum"></td><td class="linenum"><pre>66</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>67</pre></td><td><pre>    def press_button(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>68</pre></td><td><pre>        pytch.broadcast_and_wait("flash-2")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>69</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>66</pre></td><td class="linenum"><pre>70</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>67</pre></td><td class="linenum"><pre>71</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>68</pre></td><td class="linenum"><pre>72</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><h3>Right-hand button</h3><p>We can now copy and paste and make small changes to get the code for
the right-hand button, <code>Button3</code>.  Watch out for the coordinates given
to <code>self.go_to_xy()</code>, and the message given to
<code>pytch.broadcast_and_wait()</code>.</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-2")


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-3")
' data-slug="add-Button3-all-v1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>81</pre></td><td class="linenum"><pre>81</pre></td><td><pre>        self.switch_costume(1)</pre></td></tr><tr><td class="linenum"><pre>82</pre></td><td class="linenum"><pre>82</pre></td><td><pre>        self.play_sound_until_done("note-3")</pre></td></tr><tr><td class="linenum"><pre>83</pre></td><td class="linenum"><pre>83</pre></td><td><pre>        self.switch_costume(0)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-3")
'><tr><td class="linenum"></td><td class="linenum"><pre>84</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>85</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>86</pre></td><td><pre>class Button3(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>87</pre></td><td><pre>    Costumes = ["green-button.png"]</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>88</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>89</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>90</pre></td><td><pre>    def move_to_position(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>91</pre></td><td><pre>        self.go_to_xy(140, -80)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>92</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>93</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>94</pre></td><td><pre>    def press_button(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>95</pre></td><td><pre>        pytch.broadcast_and_wait("flash-3")</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Stopping the player from mashing buttons</h2><p>If you try the game now and are quick enough, you can click the
buttons quickly and make more than one light flash at once.  This
shouldn’t happen.</p><p>The program needs to be able to tell when a light is in the middle of
flashing, and not flash another light until that flash has finished.</p><p>We’ll keep track of that in a variable which stores the true/false
information</p><blockquote>
<p>is a light in the middle of flashing?</p>
</blockquote><p>Several places in the program need to use this variable, so we’ll make
it <em>global</em> by defining it outside any sprites.  At the start of the
game, no light is flashing, so the variable holds <code>False</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-1")


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-2")


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-3")
' data-slug="define-global-light-flashing"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre>pattern = [1, 2, 3, 1]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="light_flashing = False
"><tr><td class="linenum"></td><td class="linenum"><pre>4</pre></td><td><pre>light_flashing = False</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>5</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>7</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr></tbody></table></div></div><p>Starting with the when-clicked script for <code>Button1</code>, we’ll set the
variable to <code>True</code> just before the broadcast-and-wait of <code>"flash-1"</code>,
and <code>False</code> again afterwards.  We need to tell Python that we’re
talking about the <em>global</em> <code>light_flashing</code> variable:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-2")


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-3")
' data-slug="maintain-light-flashing-in-Button1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>39</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>40</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>41</pre></td><td><pre>    def press_button(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global light_flashing
        light_flashing = True
"><tr><td class="linenum"></td><td class="linenum"><pre>42</pre></td><td><pre>        global light_flashing</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>43</pre></td><td><pre>        light_flashing = True</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>44</pre></td><td><pre>        pytch.broadcast_and_wait("flash-1")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        light_flashing = False
"><tr><td class="linenum"></td><td class="linenum"><pre>45</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>43</pre></td><td class="linenum"><pre>46</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>47</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>48</pre></td><td><pre>class LED2(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>And then, <code>return</code> early if a light is already flashing:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-2")


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-3")
' data-slug="return-if-light-flashing-Button1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>40</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>41</pre></td><td><pre>    def press_button(self):</pre></td></tr><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>42</pre></td><td><pre>        global light_flashing</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        if light_flashing:
            return
"><tr><td class="linenum"></td><td class="linenum"><pre>43</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>44</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>43</pre></td><td class="linenum"><pre>45</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>46</pre></td><td><pre>        pytch.broadcast_and_wait("flash-1")</pre></td></tr><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>47</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody></table></div></div><p>We do the same thing in <code>Button2</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        pytch.broadcast_and_wait("flash-3")
' data-slug="guard-Button2-press"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>71</pre></td><td class="linenum"><pre>71</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>72</pre></td><td class="linenum"><pre>72</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>73</pre></td><td class="linenum"><pre>73</pre></td><td><pre>    def press_button(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global light_flashing
        if light_flashing:
            return
        light_flashing = True
"><tr><td class="linenum"></td><td class="linenum"><pre>74</pre></td><td><pre>        global light_flashing</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>75</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>76</pre></td><td><pre>            return</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>77</pre></td><td><pre>        light_flashing = True</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>74</pre></td><td class="linenum"><pre>78</pre></td><td><pre>        pytch.broadcast_and_wait("flash-2")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        light_flashing = False
"><tr><td class="linenum"></td><td class="linenum"><pre>79</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>75</pre></td><td class="linenum"><pre>80</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>76</pre></td><td class="linenum"><pre>81</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>77</pre></td><td class="linenum"><pre>82</pre></td><td><pre>class LED3(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>and <code>Button3</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        light_flashing = False
' data-slug="guard-Button3-press"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>103</pre></td><td class="linenum"><pre>103</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>104</pre></td><td class="linenum"><pre>104</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>105</pre></td><td class="linenum"><pre>105</pre></td><td><pre>    def press_button(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global light_flashing
        if light_flashing:
            return
        light_flashing = True
"><tr><td class="linenum"></td><td class="linenum"><pre>106</pre></td><td><pre>        global light_flashing</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>107</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>108</pre></td><td><pre>            return</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>109</pre></td><td><pre>        light_flashing = True</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>106</pre></td><td class="linenum"><pre>110</pre></td><td><pre>        pytch.broadcast_and_wait("flash-3")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        light_flashing = False
"><tr><td class="linenum"></td><td class="linenum"><pre>111</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody></table></div></div><p>(If you’re thinking ‘surely there’s a better way to write this program
than copying and pasting everything’, you’re right, but the techniques
you need are more advanced than this tutorial covers.)</p></div><div class="chapter-content"><h2>Recording the player’s attempt</h2><p>As the player clicks the buttons to try to repeat the pattern, our
program needs to remember the sequence of buttons in the player’s
attempt.</p><p>A global variable holding a list will do this for us.  At the start of
the game, the player has not pressed any buttons, so the list is
empty:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        light_flashing = False
' data-slug="define-global-user-attempt"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre>pattern = [1, 2, 3, 1]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="user_attempt = []
"><tr><td class="linenum"></td><td class="linenum"><pre>4</pre></td><td><pre>user_attempt = []</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>5</pre></td><td><pre>light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>When each button is pressed, we want to add an element to this list,
to build up a history of the player’s attempt to repeat the pattern.
Inside <code>Button1</code>, we tell Python we’ll be talking about the <em>global</em>
<code>user_attempt</code> variable as well as the global <code>light_flashing</code>
variable:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        light_flashing = False
' data-slug="resolve-user-attempt-as-global"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>40</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>41</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>42</pre></td><td><pre>    def press_button(self):</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>43</pre></td><td class="linenum"></td><td><pre>        global light_flashing</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global light_flashing, user_attempt
"><tr><td class="linenum"></td><td class="linenum"><pre>43</pre></td><td><pre>        global light_flashing, user_attempt</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>44</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>45</pre></td><td><pre>            return</pre></td></tr><tr><td class="linenum"><pre>46</pre></td><td class="linenum"><pre>46</pre></td><td><pre>        light_flashing = True</pre></td></tr></tbody></table></div></div><p>and then once the <code>"flash-1"</code> message has been responded to, we
<em>append</em> the value <code>1</code> to the list:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        light_flashing = False
' data-slug="append-1-to-user-attempt"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>45</pre></td><td><pre>            return</pre></td></tr><tr><td class="linenum"><pre>46</pre></td><td class="linenum"><pre>46</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>47</pre></td><td class="linenum"><pre>47</pre></td><td><pre>        pytch.broadcast_and_wait("flash-1")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        user_attempt.append(1)
"><tr><td class="linenum"></td><td class="linenum"><pre>48</pre></td><td><pre>        user_attempt.append(1)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>48</pre></td><td class="linenum"><pre>49</pre></td><td><pre>        light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>49</pre></td><td class="linenum"><pre>50</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>50</pre></td><td class="linenum"><pre>51</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>We do something very similar in <code>Button2</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        light_flashing = False
' data-slug="append-2-to-user-attempt"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>73</pre></td><td class="linenum"><pre>73</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>74</pre></td><td class="linenum"><pre>74</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>75</pre></td><td class="linenum"><pre>75</pre></td><td><pre>    def press_button(self):</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>76</pre></td><td class="linenum"></td><td><pre>        global light_flashing</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global light_flashing, user_attempt
"><tr><td class="linenum"></td><td class="linenum"><pre>76</pre></td><td><pre>        global light_flashing, user_attempt</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>77</pre></td><td class="linenum"><pre>77</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"><pre>78</pre></td><td class="linenum"><pre>78</pre></td><td><pre>            return</pre></td></tr><tr><td class="linenum"><pre>79</pre></td><td class="linenum"><pre>79</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>80</pre></td><td class="linenum"><pre>80</pre></td><td><pre>        pytch.broadcast_and_wait("flash-2")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        user_attempt.append(2)
"><tr><td class="linenum"></td><td class="linenum"><pre>81</pre></td><td><pre>        user_attempt.append(2)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>81</pre></td><td class="linenum"><pre>82</pre></td><td><pre>        light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>82</pre></td><td class="linenum"><pre>83</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>83</pre></td><td class="linenum"><pre>84</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>and in <code>Button3</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="append-3-to-user-attempt"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>106</pre></td><td class="linenum"><pre>106</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>107</pre></td><td class="linenum"><pre>107</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>108</pre></td><td class="linenum"><pre>108</pre></td><td><pre>    def press_button(self):</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>109</pre></td><td class="linenum"></td><td><pre>        global light_flashing</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global light_flashing, user_attempt
"><tr><td class="linenum"></td><td class="linenum"><pre>109</pre></td><td><pre>        global light_flashing, user_attempt</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>110</pre></td><td class="linenum"><pre>110</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"><pre>111</pre></td><td class="linenum"><pre>111</pre></td><td><pre>            return</pre></td></tr><tr><td class="linenum"><pre>112</pre></td><td class="linenum"><pre>112</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>113</pre></td><td class="linenum"><pre>113</pre></td><td><pre>        pytch.broadcast_and_wait("flash-3")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        user_attempt.append(3)
"><tr><td class="linenum"></td><td class="linenum"><pre>114</pre></td><td><pre>        user_attempt.append(3)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>114</pre></td><td class="linenum"><pre>115</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Checking whether the player is getting it right</h2><p>Each time the player adds to the pattern, we need to check if they’re
right so far.  We also need to check whether they’ve now done the
whole pattern correctly.</p><p>This is quite a fiddly piece of code, so we’ll print some messages to
the <code>Output</code> tab, to check the code is behaving as we want.</p><p>We’ll start by defining a function which will run in response to a
message <code>"check-user-attempt"</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="define-check-user-attempt-function"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>15</pre></td><td><pre>            pytch.broadcast_and_wait(message)</pre></td></tr><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>16</pre></td><td><pre>            pytch.wait_seconds(0.1)</pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):

'><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre>    @pytch.when_I_receive("check-user-attempt")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>19</pre></td><td><pre>    def check_user_attempt(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>20</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>21</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>22</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>23</pre></td><td><pre>    Costumes = ["light-off.png", "light-on.png"]</pre></td></tr></tbody></table></div></div><p>In the body of this function, want to ask the question</p><blockquote>
<p>Is what the user has done so far correct?</p>
</blockquote><p>Suppose the user has pressed four buttons so far.  Then we want to
compare the <code>user_attempt</code> list to a list containing the first four
items in the <code>pattern</code> list.  We’ll use Python <em>slicing</em> to extract a
section of a list.  We find out how many items are in the
<code>user_attempt</code> list, and then copy that many items from the start of
<code>pattern</code> into a local list variable <code>pattern_start</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="compute-pattern-start"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>18</pre></td><td><pre>    @pytch.when_I_receive("check-user-attempt")</pre></td></tr><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>19</pre></td><td><pre>    def check_user_attempt(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
"><tr><td class="linenum"></td><td class="linenum"><pre>20</pre></td><td><pre>        user_attempt_length = len(user_attempt)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>21</pre></td><td><pre>        pattern_start = pattern[:user_attempt_length]</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>22</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>23</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>24</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>So we can check our code is working, we’ll print the lists we’re
interested in:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="print-pattern-user-attempt-pattern-start"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>19</pre></td><td><pre>    def check_user_attempt(self):</pre></td></tr><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>20</pre></td><td><pre>        user_attempt_length = len(user_attempt)</pre></td></tr><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>21</pre></td><td><pre>        pattern_start = pattern[:user_attempt_length]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
'><tr><td class="linenum"></td><td class="linenum"><pre>22</pre></td><td><pre>        print("pattern is", pattern)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>23</pre></td><td><pre>        print("user_attempt is", user_attempt)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>24</pre></td><td><pre>        print("pattern_start is", pattern_start)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>25</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>26</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>27</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>We can now ask whether the player’s attempt is equal to the first part
of the pattern they’re meant to be copying.  For now, we’ll print a
message saying whether they’re right or wrong:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="print-fail-or-ok-so-far"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>22</pre></td><td><pre>        print("pattern is", pattern)</pre></td></tr><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>23</pre></td><td><pre>        print("user_attempt is", user_attempt)</pre></td></tr><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>24</pre></td><td><pre>        print("pattern_start is", pattern_start)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='        if user_attempt == pattern_start:
            print("OK so far")
        else:
            print("FAIL")
'><tr><td class="linenum"></td><td class="linenum"><pre>25</pre></td><td><pre>        if user_attempt == pattern_start:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>26</pre></td><td><pre>            print("OK so far")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>27</pre></td><td><pre>        else:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>28</pre></td><td><pre>            print("FAIL")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>29</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>26</pre></td><td class="linenum"><pre>30</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>27</pre></td><td class="linenum"><pre>31</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>If they’re right so far, we can go on to check whether they’ve in fact
got the whole pattern right.  We can test this by comparing the length
of the user pattern to the length of the original pattern — if they’re
the same, the player has got the whole pattern right.  Again, for now
we just print a message if the player has got everything right:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="maybe-print-whole-pattern-OK"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>24</pre></td><td><pre>        print("pattern_start is", pattern_start)</pre></td></tr><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>25</pre></td><td><pre>        if user_attempt == pattern_start:</pre></td></tr><tr><td class="linenum"><pre>26</pre></td><td class="linenum"><pre>26</pre></td><td><pre>            print("OK so far")</pre></td></tr></tbody><tbody class="diff-add" data-added-text='            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
'><tr><td class="linenum"></td><td class="linenum"><pre>27</pre></td><td><pre>            if user_attempt_length == len(pattern):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>28</pre></td><td><pre>                print("Whole pattern OK")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>27</pre></td><td class="linenum"><pre>29</pre></td><td><pre>        else:</pre></td></tr><tr><td class="linenum"><pre>28</pre></td><td class="linenum"><pre>30</pre></td><td><pre>            print("FAIL")</pre></td></tr><tr><td class="linenum"><pre>29</pre></td><td class="linenum"><pre>31</pre></td><td><pre></pre></td></tr></tbody></table></div></div><h3>Checking after each button click</h3><p>But our program is not yet <em>running</em> this code, because nothing is
broadcasting the <code>"check-user-attempt"</code> message.</p><p>Each button needs to broadcast this message after adding its number to
the <code>user_attempt</code> list.  We add code to <code>Button1</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="check-after-Button1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>60</pre></td><td class="linenum"><pre>60</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>61</pre></td><td class="linenum"><pre>61</pre></td><td><pre>        pytch.broadcast_and_wait("flash-1")</pre></td></tr><tr><td class="linenum"><pre>62</pre></td><td class="linenum"><pre>62</pre></td><td><pre>        user_attempt.append(1)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='        pytch.broadcast_and_wait("check-user-attempt")
'><tr><td class="linenum"></td><td class="linenum"><pre>63</pre></td><td><pre>        pytch.broadcast_and_wait("check-user-attempt")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>63</pre></td><td class="linenum"><pre>64</pre></td><td><pre>        light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>64</pre></td><td class="linenum"><pre>65</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>65</pre></td><td class="linenum"><pre>66</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>and <code>Button2</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        light_flashing = False
' data-slug="check-after-Button2"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>94</pre></td><td class="linenum"><pre>94</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>95</pre></td><td class="linenum"><pre>95</pre></td><td><pre>        pytch.broadcast_and_wait("flash-2")</pre></td></tr><tr><td class="linenum"><pre>96</pre></td><td class="linenum"><pre>96</pre></td><td><pre>        user_attempt.append(2)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='        pytch.broadcast_and_wait("check-user-attempt")
'><tr><td class="linenum"></td><td class="linenum"><pre>97</pre></td><td><pre>        pytch.broadcast_and_wait("check-user-attempt")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>97</pre></td><td class="linenum"><pre>98</pre></td><td><pre>        light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>98</pre></td><td class="linenum"><pre>99</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>99</pre></td><td class="linenum"><pre>100</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>and <code>Button3</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_key_pressed("p")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="check-after-Button3"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>128</pre></td><td class="linenum"><pre>128</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>129</pre></td><td class="linenum"><pre>129</pre></td><td><pre>        pytch.broadcast_and_wait("flash-3")</pre></td></tr><tr><td class="linenum"><pre>130</pre></td><td class="linenum"><pre>130</pre></td><td><pre>        user_attempt.append(3)</pre></td></tr></tbody><tbody class="diff-add" data-added-text='        pytch.broadcast_and_wait("check-user-attempt")
'><tr><td class="linenum"></td><td class="linenum"><pre>131</pre></td><td><pre>        pytch.broadcast_and_wait("check-user-attempt")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>131</pre></td><td class="linenum"><pre>132</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody></table></div></div><h3>Test the code</h3><p>You can try this now — press <code>p</code> and check what’s printed in the
<code>Output</code> tab as you click on the buttons.</p></div><div class="chapter-content"><h2>Running the game</h2><p>We can now start to replace some of our ‘just for now’ code with real
code.</p><p>The code which plays the pattern is currently launched by pressing the
<code>p</code> key.  Instead, we'll make it run when it receives a
<code>"play-pattern"</code> message:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = [1, 2, 3, 1]
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="play-pattern-on-message-not-keypress"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>11</pre></td><td class="linenum"></td><td><pre>    @pytch.when_key_pressed("p")</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_I_receive("play-pattern")
'><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_I_receive("play-pattern")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    def play_pattern(self):</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>        for led in pattern:</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre>            message = "flash-" + str(led)</pre></td></tr></tbody></table></div></div><p>At the start of the game, there are no lights in the pattern, so
<code>pattern</code> should start off as the empty list:</p><div class="patch-container" data-code-as-of-commit='import pytch

pattern = []
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="init-pattern-to-empty-list"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>3</pre></td><td class="linenum"></td><td><pre>pattern = [1, 2, 3, 1]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="pattern = []
"><tr><td class="linenum"></td><td class="linenum"><pre>3</pre></td><td><pre>pattern = []</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre>user_attempt = []</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>5</pre></td><td><pre>light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr></tbody></table></div></div><h3>Building up a random pattern</h3><p>To be able to choose random numbers, we need to <code>import</code> Python’s
library for working with random numbers.  This is like adding an
extension in Scratch.  We want to use the code provided in the
<code>random</code> library:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="import-random"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody><tbody class="diff-add" data-added-text="import random
"><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre>import random</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>4</pre></td><td><pre>pattern = []</pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>5</pre></td><td><pre>user_attempt = []</pre></td></tr></tbody></table></div></div><p>Now we’ll put together a function which adds a new random light to the
pattern, then makes the new pattern play.  In the next block of code,
you should be able to find which line:</p><ul>
<li>makes sure Python knows we’re talking about the <em>global</em> <code>pattern</code>
  variable</li>
<li>uses the <code>randint()</code> function in the <code>random</code> library to choose a
  random whole number between 1 and 3</li>
<li>adds that number to the end of the <code>pattern</code> list</li>
<li>broadcasts the right message to make the pattern play.</li>
</ul><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="add-flash-and-play-v1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")

'><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>    @pytch.when_I_receive("add-flash-and-play")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>    def add_flash_and_play(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>14</pre></td><td><pre>        global pattern</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>15</pre></td><td><pre>        new_light = random.randint(1, 3)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>16</pre></td><td><pre>        pattern.append(new_light)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>17</pre></td><td><pre>        pytch.broadcast_and_wait("play-pattern")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>19</pre></td><td><pre>    @pytch.when_I_receive("play-pattern")</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>20</pre></td><td><pre>    def play_pattern(self):</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>21</pre></td><td><pre>        for led in pattern:</pre></td></tr></tbody></table></div></div><h3>Extending the pattern when the player gets the old pattern right</h3><p>Jumping back to the code which tests whether the player has got the
pattern completely right, this is exactly when we should add a new
light-flash to the end of the pattern.  We also need to set
<code>user_attempt</code> to the empty list, ready to build up the player’s
attempt at the new, longer pattern.  The <code>clear()</code> method on a list
does this.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="next-pattern-when-success-so-far"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>34</pre></td><td><pre>            print("OK so far")</pre></td></tr><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>35</pre></td><td><pre>            if user_attempt_length == len(pattern):</pre></td></tr><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>36</pre></td><td><pre>                print("Whole pattern OK")</pre></td></tr></tbody><tbody class="diff-add" data-added-text='                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pytch.broadcast_and_wait("add-flash-and-play")
'><tr><td class="linenum"></td><td class="linenum"><pre>37</pre></td><td><pre>                pytch.wait_seconds(0.25)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>38</pre></td><td><pre>                user_attempt.clear()</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>39</pre></td><td><pre>                pytch.broadcast_and_wait("add-flash-and-play")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>40</pre></td><td><pre>        else:</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>41</pre></td><td><pre>            print("FAIL")</pre></td></tr><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>42</pre></td><td><pre></pre></td></tr></tbody></table></div></div><h3>Starting the whole game off</h3><p>Finally, we start the game by broadcasting the message to add a
light-flash and let the player try to copy it.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="start-game-by-adding"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>class Background(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre>    Backdrops = ["blue-gradient.png"]</pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

'><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>    def play_game(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>14</pre></td><td><pre>        pytch.broadcast("add-flash-and-play")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>15</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>16</pre></td><td><pre>    @pytch.when_I_receive("add-flash-and-play")</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>17</pre></td><td><pre>    def add_flash_and_play(self):</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>18</pre></td><td><pre>        global pattern</pre></td></tr></tbody></table></div></div><p>Try this now!</p></div><div class="chapter-content"><h2>Only allowing button pressing at right times</h2><p>You might notice that the player can click the buttons at any time,
even when the game is showing the player the pattern.  We’ll fix that
now.</p><p>There are some times in the game when pressing the buttons is allowed,
and some times when it is not allowed.  We’ll make a global variable
to keep track of the true/false information</p><blockquote>
<p>is the player allowed to press a button right now?</p>
</blockquote><p>This starts off <code>False</code>, because the player isn’t allowed to press a
button until the first pattern has been played.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="define-global-pressing-allowed"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre>pattern = []</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>5</pre></td><td><pre>user_attempt = []</pre></td></tr></tbody><tbody class="diff-add" data-added-text="pressing_allowed = False
"><tr><td class="linenum"></td><td class="linenum"><pre>6</pre></td><td><pre>pressing_allowed = False</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>7</pre></td><td><pre>light_flashing = False</pre></td></tr><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>8</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>9</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>Once the pattern has been played, the player is allowed to press the
buttons, to try to copy the pattern.  We tell Python we’re talking
about the <em>global</em> <code>pressing_allowed</code> variable.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="set-pressing-allowed-after-play-pattern"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre>    @pytch.when_I_receive("add-flash-and-play")</pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>18</pre></td><td><pre>    def add_flash_and_play(self):</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>19</pre></td><td class="linenum"></td><td><pre>        global pattern</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global pattern, pressing_allowed
"><tr><td class="linenum"></td><td class="linenum"><pre>19</pre></td><td><pre>        global pattern, pressing_allowed</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>20</pre></td><td><pre>        new_light = random.randint(1, 3)</pre></td></tr><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>21</pre></td><td><pre>        pattern.append(new_light)</pre></td></tr><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>22</pre></td><td><pre>        pytch.broadcast_and_wait("play-pattern")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        pressing_allowed = True
"><tr><td class="linenum"></td><td class="linenum"><pre>23</pre></td><td><pre>        pressing_allowed = True</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>24</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>25</pre></td><td><pre>    @pytch.when_I_receive("play-pattern")</pre></td></tr><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>26</pre></td><td><pre>    def play_pattern(self):</pre></td></tr></tbody></table></div></div><p>In the <code>check_user_attempt()</code> function, if the user gets the whole
pattern right, they’re then <em>not</em> allowed to press any buttons just
yet.  There are two changes needed in this function:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="clear-pressing-allowed-when-player-repeats-ok"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>31</pre></td><td class="linenum"><pre>31</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>32</pre></td><td class="linenum"><pre>32</pre></td><td><pre>    @pytch.when_I_receive("check-user-attempt")</pre></td></tr><tr><td class="linenum"><pre>33</pre></td><td class="linenum"><pre>33</pre></td><td><pre>    def check_user_attempt(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global pressing_allowed
"><tr><td class="linenum"></td><td class="linenum"><pre>34</pre></td><td><pre>        global pressing_allowed</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>35</pre></td><td><pre>        user_attempt_length = len(user_attempt)</pre></td></tr><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>36</pre></td><td><pre>        pattern_start = pattern[:user_attempt_length]</pre></td></tr><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        print("pattern is", pattern)</pre></td></tr></tbody></table><table><tbody class="diff-unch"><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>43</pre></td><td><pre>                print("Whole pattern OK")</pre></td></tr><tr><td class="linenum"><pre>43</pre></td><td class="linenum"><pre>44</pre></td><td><pre>                pytch.wait_seconds(0.25)</pre></td></tr><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>45</pre></td><td><pre>                user_attempt.clear()</pre></td></tr></tbody><tbody class="diff-add" data-added-text="                pressing_allowed = False
"><tr><td class="linenum"></td><td class="linenum"><pre>46</pre></td><td><pre>                pressing_allowed = False</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>47</pre></td><td><pre>                pytch.broadcast_and_wait("add-flash-and-play")</pre></td></tr><tr><td class="linenum"><pre>46</pre></td><td class="linenum"><pre>48</pre></td><td><pre>        else:</pre></td></tr><tr><td class="linenum"><pre>47</pre></td><td class="linenum"><pre>49</pre></td><td><pre>            print("FAIL")</pre></td></tr></tbody></table></div></div><p>If the user fails, by getting a light wrong, they’re not allowed to
press buttons:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="clear-pressing-allowed-when-player-fails"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>47</pre></td><td class="linenum"><pre>47</pre></td><td><pre>                pytch.broadcast_and_wait("add-flash-and-play")</pre></td></tr><tr><td class="linenum"><pre>48</pre></td><td class="linenum"><pre>48</pre></td><td><pre>        else:</pre></td></tr><tr><td class="linenum"><pre>49</pre></td><td class="linenum"><pre>49</pre></td><td><pre>            print("FAIL")</pre></td></tr></tbody><tbody class="diff-add" data-added-text="            pressing_allowed = False
"><tr><td class="linenum"></td><td class="linenum"><pre>50</pre></td><td><pre>            pressing_allowed = False</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>50</pre></td><td class="linenum"><pre>51</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>51</pre></td><td class="linenum"><pre>52</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>52</pre></td><td class="linenum"><pre>53</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>So far we’re just setting this variable to different values.  We now
make the <code>Button1</code>, <code>Button2</code>, and <code>Button3</code> sprites abandon their
when-clicked script early if pressing is not allowed.</p><p>For <code>Button1</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="return-if-pressing-disallowed-Button1"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>77</pre></td><td class="linenum"><pre>77</pre></td><td><pre>        global light_flashing, user_attempt</pre></td></tr><tr><td class="linenum"><pre>78</pre></td><td class="linenum"><pre>78</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"><pre>79</pre></td><td class="linenum"><pre>79</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        if not pressing_allowed:
            return
"><tr><td class="linenum"></td><td class="linenum"><pre>80</pre></td><td><pre>        if not pressing_allowed:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>81</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>80</pre></td><td class="linenum"><pre>82</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>81</pre></td><td class="linenum"><pre>83</pre></td><td><pre>        pytch.broadcast_and_wait("flash-1")</pre></td></tr><tr><td class="linenum"><pre>82</pre></td><td class="linenum"><pre>84</pre></td><td><pre>        user_attempt.append(1)</pre></td></tr></tbody></table></div></div><p>and <code>Button2</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="return-if-pressing-disallowed-Button2"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>113</pre></td><td class="linenum"><pre>113</pre></td><td><pre>        global light_flashing, user_attempt</pre></td></tr><tr><td class="linenum"><pre>114</pre></td><td class="linenum"><pre>114</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"><pre>115</pre></td><td class="linenum"><pre>115</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        if not pressing_allowed:
            return
"><tr><td class="linenum"></td><td class="linenum"><pre>116</pre></td><td><pre>        if not pressing_allowed:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>117</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>116</pre></td><td class="linenum"><pre>118</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>117</pre></td><td class="linenum"><pre>119</pre></td><td><pre>        pytch.broadcast_and_wait("flash-2")</pre></td></tr><tr><td class="linenum"><pre>118</pre></td><td class="linenum"><pre>120</pre></td><td><pre>        user_attempt.append(2)</pre></td></tr></tbody></table></div></div><p>and <code>Button3</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False
' data-slug="return-if-pressing-disallowed-Button3"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>149</pre></td><td class="linenum"><pre>149</pre></td><td><pre>        global light_flashing, user_attempt</pre></td></tr><tr><td class="linenum"><pre>150</pre></td><td class="linenum"><pre>150</pre></td><td><pre>        if light_flashing:</pre></td></tr><tr><td class="linenum"><pre>151</pre></td><td class="linenum"><pre>151</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        if not pressing_allowed:
            return
"><tr><td class="linenum"></td><td class="linenum"><pre>152</pre></td><td><pre>        if not pressing_allowed:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>153</pre></td><td><pre>            return</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>152</pre></td><td class="linenum"><pre>154</pre></td><td><pre>        light_flashing = True</pre></td></tr><tr><td class="linenum"><pre>153</pre></td><td class="linenum"><pre>155</pre></td><td><pre>        pytch.broadcast_and_wait("flash-3")</pre></td></tr><tr><td class="linenum"><pre>154</pre></td><td class="linenum"><pre>156</pre></td><td><pre>        user_attempt.append(3)</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Using in-game messages</h2><p>Watching for text being printed in the <code>Output</code> tab is not great.
We’ll add some in-game messages.</p><p>Add a new sprite which has a costume for each message we want to show:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]
' data-slug="add-Text-and-costumes"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>156</pre></td><td class="linenum"><pre>156</pre></td><td><pre>        user_attempt.append(3)</pre></td></tr><tr><td class="linenum"><pre>157</pre></td><td class="linenum"><pre>157</pre></td><td><pre>        pytch.broadcast_and_wait("check-user-attempt")</pre></td></tr><tr><td class="linenum"><pre>158</pre></td><td class="linenum"><pre>158</pre></td><td><pre>        light_flashing = False</pre></td></tr></tbody><tbody class="diff-add" data-added-text='

class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>159</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>160</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>161</pre></td><td><pre>class Text(pytch.Sprite):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>162</pre></td><td><pre>    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]</pre></td></tr></tbody></table></div></div><p>At the start of the game, there is no message shown, so the <code>Text</code>
sprite should hide itself:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()
' data-slug="init-Text-hidden"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>160</pre></td><td class="linenum"><pre>160</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>161</pre></td><td class="linenum"><pre>161</pre></td><td><pre>class Text(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>162</pre></td><td class="linenum"><pre>162</pre></td><td><pre>    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()
"><tr><td class="linenum"></td><td class="linenum"><pre>163</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>164</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>165</pre></td><td><pre>    def init(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>166</pre></td><td><pre>        self.hide()</pre></td></tr></tbody></table></div></div><p>This sprite will flash its ‘YES!’ costume when it receives the
<code>"correct"</code> message:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()

    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()
' data-slug="flash-YES-on-message"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>164</pre></td><td class="linenum"><pre>164</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"><pre>165</pre></td><td class="linenum"><pre>165</pre></td><td><pre>    def init(self):</pre></td></tr><tr><td class="linenum"><pre>166</pre></td><td class="linenum"><pre>166</pre></td><td><pre>        self.hide()</pre></td></tr></tbody><tbody class="diff-add" data-added-text='
    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()
'><tr><td class="linenum"></td><td class="linenum"><pre>167</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>168</pre></td><td><pre>    @pytch.when_I_receive("correct")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>169</pre></td><td><pre>    def flash_yes(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>170</pre></td><td><pre>        self.switch_costume("YES-neon-text")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>171</pre></td><td><pre>        self.show()</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>172</pre></td><td><pre>        pytch.wait_seconds(1.0)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>173</pre></td><td><pre>        self.hide()</pre></td></tr></tbody></table></div></div><p>and show its ‘FAIL’ costume when it receives <code>"fail"</code>:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()

    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()

    @pytch.when_I_receive("fail")
    def flash_fail(self):
        self.switch_costume("FAIL-neon-text")
        self.show()
' data-slug="flash-FAIL-on-message"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>171</pre></td><td class="linenum"><pre>171</pre></td><td><pre>        self.show()</pre></td></tr><tr><td class="linenum"><pre>172</pre></td><td class="linenum"><pre>172</pre></td><td><pre>        pytch.wait_seconds(1.0)</pre></td></tr><tr><td class="linenum"><pre>173</pre></td><td class="linenum"><pre>173</pre></td><td><pre>        self.hide()</pre></td></tr></tbody><tbody class="diff-add" data-added-text='
    @pytch.when_I_receive("fail")
    def flash_fail(self):
        self.switch_costume("FAIL-neon-text")
        self.show()
'><tr><td class="linenum"></td><td class="linenum"><pre>174</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>175</pre></td><td><pre>    @pytch.when_I_receive("fail")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>176</pre></td><td><pre>    def flash_fail(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>177</pre></td><td><pre>        self.switch_costume("FAIL-neon-text")</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>178</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div><p>Here there’s no need to wait and then hide, because once the player
has failed, the whole game is over, so the message can stay.</p><h3>Broadcasting the messages</h3><p>Back inside <code>check_user_attempt()</code>, we broadcast the <code>"correct"</code>
message when the player has got the whole pattern right:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.broadcast_and_wait("correct")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()

    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()

    @pytch.when_I_receive("fail")
    def flash_fail(self):
        self.switch_costume("FAIL-neon-text")
        self.show()
' data-slug="launch-YES-on-success"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>41</pre></td><td><pre>            print("OK so far")</pre></td></tr><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>42</pre></td><td><pre>            if user_attempt_length == len(pattern):</pre></td></tr><tr><td class="linenum"><pre>43</pre></td><td class="linenum"><pre>43</pre></td><td><pre>                print("Whole pattern OK")</pre></td></tr></tbody><tbody class="diff-add" data-added-text='                pytch.broadcast_and_wait("correct")
'><tr><td class="linenum"></td><td class="linenum"><pre>44</pre></td><td><pre>                pytch.broadcast_and_wait("correct")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>45</pre></td><td><pre>                pytch.wait_seconds(0.25)</pre></td></tr><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>46</pre></td><td><pre>                user_attempt.clear()</pre></td></tr><tr><td class="linenum"><pre>46</pre></td><td class="linenum"><pre>47</pre></td><td><pre>                pressing_allowed = False</pre></td></tr></tbody></table></div></div><p>and the <code>"fail"</code> message if they make a mistake:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        print("pattern is", pattern)
        print("user_attempt is", user_attempt)
        print("pattern_start is", pattern_start)
        if user_attempt == pattern_start:
            print("OK so far")
            if user_attempt_length == len(pattern):
                print("Whole pattern OK")
                pytch.broadcast_and_wait("correct")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            print("FAIL")
            pressing_allowed = False
            pytch.broadcast("fail")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()

    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()

    @pytch.when_I_receive("fail")
    def flash_fail(self):
        self.switch_costume("FAIL-neon-text")
        self.show()
' data-slug="launch-FAIL-on-failure"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>49</pre></td><td class="linenum"><pre>49</pre></td><td><pre>        else:</pre></td></tr><tr><td class="linenum"><pre>50</pre></td><td class="linenum"><pre>50</pre></td><td><pre>            print("FAIL")</pre></td></tr><tr><td class="linenum"><pre>51</pre></td><td class="linenum"><pre>51</pre></td><td><pre>            pressing_allowed = False</pre></td></tr></tbody><tbody class="diff-add" data-added-text='            pytch.broadcast("fail")
'><tr><td class="linenum"></td><td class="linenum"><pre>52</pre></td><td><pre>            pytch.broadcast("fail")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>52</pre></td><td class="linenum"><pre>53</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>53</pre></td><td class="linenum"><pre>54</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>54</pre></td><td class="linenum"><pre>55</pre></td><td><pre>class LED1(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>Try this now!</p></div><div class="chapter-content"><h2>Tidying up</h2><p>Now everything is working, we can delete the <code>print()</code> lines:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random

pattern = []
user_attempt = []
pressing_allowed = False
light_flashing = False


class Background(pytch.Stage):
    Backdrops = ["blue-gradient.png"]

    @pytch.when_green_flag_clicked
    def play_game(self):
        pytch.broadcast("add-flash-and-play")

    @pytch.when_I_receive("add-flash-and-play")
    def add_flash_and_play(self):
        global pattern, pressing_allowed
        new_light = random.randint(1, 3)
        pattern.append(new_light)
        pytch.broadcast_and_wait("play-pattern")
        pressing_allowed = True

    @pytch.when_I_receive("play-pattern")
    def play_pattern(self):
        for led in pattern:
            message = "flash-" + str(led)
            pytch.broadcast_and_wait(message)
            pytch.wait_seconds(0.1)

    @pytch.when_I_receive("check-user-attempt")
    def check_user_attempt(self):
        global pressing_allowed
        user_attempt_length = len(user_attempt)
        pattern_start = pattern[:user_attempt_length]
        if user_attempt == pattern_start:
            if user_attempt_length == len(pattern):
                pytch.broadcast_and_wait("correct")
                pytch.wait_seconds(0.25)
                user_attempt.clear()
                pressing_allowed = False
                pytch.broadcast_and_wait("add-flash-and-play")
        else:
            pressing_allowed = False
            pytch.broadcast("fail")


class LED1(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-1.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, 80)

    @pytch.when_I_receive("flash-1")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-1")
        self.switch_costume(0)


class Button1(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(-140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-1")
        user_attempt.append(1)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED2(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-2.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, 80)

    @pytch.when_I_receive("flash-2")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-2")
        self.switch_costume(0)


class Button2(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(0, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-2")
        user_attempt.append(2)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class LED3(pytch.Sprite):
    Costumes = ["light-off.png", "light-on.png"]
    Sounds = ["note-3.mp3"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, 80)

    @pytch.when_I_receive("flash-3")
    def flash(self):
        self.switch_costume(1)
        self.play_sound_until_done("note-3")
        self.switch_costume(0)


class Button3(pytch.Sprite):
    Costumes = ["green-button.png"]

    @pytch.when_green_flag_clicked
    def move_to_position(self):
        self.go_to_xy(140, -80)

    @pytch.when_this_sprite_clicked
    def press_button(self):
        global light_flashing, user_attempt
        if light_flashing:
            return
        if not pressing_allowed:
            return
        light_flashing = True
        pytch.broadcast_and_wait("flash-3")
        user_attempt.append(3)
        pytch.broadcast_and_wait("check-user-attempt")
        light_flashing = False


class Text(pytch.Sprite):
    Costumes = ["YES-neon-text.png", "FAIL-neon-text.png"]

    @pytch.when_green_flag_clicked
    def init(self):
        self.hide()

    @pytch.when_I_receive("correct")
    def flash_yes(self):
        self.switch_costume("YES-neon-text")
        self.show()
        pytch.wait_seconds(1.0)
        self.hide()

    @pytch.when_I_receive("fail")
    def flash_fail(self):
        self.switch_costume("FAIL-neon-text")
        self.show()
' data-slug="remove-prints"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>34</pre></td><td class="linenum"><pre>34</pre></td><td><pre>        global pressing_allowed</pre></td></tr><tr><td class="linenum"><pre>35</pre></td><td class="linenum"><pre>35</pre></td><td><pre>        user_attempt_length = len(user_attempt)</pre></td></tr><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>36</pre></td><td><pre>        pattern_start = pattern[:user_attempt_length]</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>37</pre></td><td class="linenum"></td><td><pre>        print("pattern is", pattern)</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"></td><td><pre>        print("user_attempt is", user_attempt)</pre></td></tr><tr><td class="linenum"><pre>39</pre></td><td class="linenum"></td><td><pre>        print("pattern_start is", pattern_start)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        if user_attempt == pattern_start:</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>41</pre></td><td class="linenum"></td><td><pre>            print("OK so far")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>42</pre></td><td class="linenum"><pre>38</pre></td><td><pre>            if user_attempt_length == len(pattern):</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>43</pre></td><td class="linenum"></td><td><pre>                print("Whole pattern OK")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>44</pre></td><td class="linenum"><pre>39</pre></td><td><pre>                pytch.broadcast_and_wait("correct")</pre></td></tr><tr><td class="linenum"><pre>45</pre></td><td class="linenum"><pre>40</pre></td><td><pre>                pytch.wait_seconds(0.25)</pre></td></tr><tr><td class="linenum"><pre>46</pre></td><td class="linenum"><pre>41</pre></td><td><pre>                user_attempt.clear()</pre></td></tr><tr><td class="linenum"><pre>47</pre></td><td class="linenum"><pre>42</pre></td><td><pre>                pressing_allowed = False</pre></td></tr><tr><td class="linenum"><pre>48</pre></td><td class="linenum"><pre>43</pre></td><td><pre>                pytch.broadcast_and_wait("add-flash-and-play")</pre></td></tr><tr><td class="linenum"><pre>49</pre></td><td class="linenum"><pre>44</pre></td><td><pre>        else:</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>50</pre></td><td class="linenum"></td><td><pre>            print("FAIL")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>51</pre></td><td class="linenum"><pre>45</pre></td><td><pre>            pressing_allowed = False</pre></td></tr><tr><td class="linenum"><pre>52</pre></td><td class="linenum"><pre>46</pre></td><td><pre>            pytch.broadcast("fail")</pre></td></tr><tr><td class="linenum"><pre>53</pre></td><td class="linenum"><pre>47</pre></td><td><pre></pre></td></tr></tbody></table></div></div><p>Congratulations!  See how long a pattern you can remember!</p></div><div class="chapter-content"><h2>Make your own improvements</h2><p>Here are some ways you could change or improve the game.  Or try out
some ideas of your own!</p><h3>Show the player how they’re doing</h3><p>Add a variable which keeps track of how many light-flashes the player
has got right.  You’ll need to work out what number the variable
should hold at the start of the game, and when it changes.</p><p>You can show a global variable called <code>score</code> by putting code like
this into a green-flag script:</p><pre><code class="language-python">pytch.show_variable(None, "score")
</code></pre><p>The name of the variable is given as a string here.</p><h3>Use different graphics</h3><p>Find some images to use instead of the lights and buttons.  You’ll
need to think about how to show your lights ‘lighting up’.  Maybe the
image could get a bit bigger, or maybe you could have characters which
jump up and down instead of lights which flash.</p><h3>Change the layout of the game</h3><p>Can you think of other ways of arranging the lights and buttons on the
stage?</p><h3>Add another light and button</h3><p>Can you see how to extend the code to have four lights and buttons?
There's a sound file <code>note-4.mp3</code> you can use if you like.</p><h3>Change the speed of the game</h3><p>To play the pattern more slowly, you can increase the <code>0.1</code> in the
<code>pytch.wait_seconds(0.1)</code> line.</p><p>To play the pattern more quickly, you can reduce that <code>0.1</code>, or even
get rid of the <code>pytch.wait_seconds()</code> line altogether.  But even
without a <code>pytch.wait_seconds()</code>, each note will still play in full.
To make the game faster than that, you’ll need to think harder.</p><p>Hint: To cut the sound short, you could do something like</p><pre><code class="language-python">self.start_sound("note-1")
pytch.wait_seconds(0.3)
pytch.stop_all_sounds()
</code></pre><p>instead of the simple <code>self.play_sound_until_done("note-1")</code>.</p><h3>Add a time limit (advanced)</h3><p>Add a timer so that the player has to press the buttons quickly
enough.  For example, you could make it so that if the player waits
for more than two seconds before pressing a button, they lose.</p></div></div>